﻿//查询列表页面
function viewModel(data) {
    var self = this;
    this.dataSource = data.dataSource;
    this.form = ko.mapping.fromJS(data.form);
    this.grid = {
        size: { w: 4, h: 94 },
        url: '/api/pacm/Djcgl',
        queryParams: ko.observable(),
        pagination: true,
        customLoad: false,
        idField: 'Djcgll_BillNo'
    };
    this.gridEdit = new com.editTreeGridViewModel(this.grid);
    this.grid.onDblClickRow = self.gridEdit.begin;
    this.grid.onClickRow = self.gridEdit.ended;
    this.grid.OnAfterCreateEditor = function (edt) {
        com.readOnlyHandler('input')(edt["Djcgll_BillNo"].target, true);
    };
    //刷新
    this.refreshClick = function () {
        window.location.reload();
    };
    //增加
    this.addClick = function () {
        com.ajax({
            type: 'GET',
            url: "/api/pacm/Djcgl/GetNewBillNo",
            success: function (d) {
                var syear = data.dataSource.curYear;
                var smonth = data.dataSource.curMonth;
                var row = { Djcgll_BillNo: d, Djcgll_Year: syear, Djcgll_Month: smonth };
                self.gridEdit.addnew(row);
            }
        });
    }
    this.editClick = function () {
        var row = self.grid.treegrid('getSelected');
        //        if (row["ApproveState"] == undefined) {
        //            return;
        //        }
        self.gridEdit.begin(row);
    };
    //审核
    this.auditClick = function () {
        var row = self.grid.treegrid('getSelected');
        if (!row) return com.message('warning', "请选择一条明细数据!");
        com.auditDialog(function (d) {
            com.ajax({
                type: 'POST',
                url: "/api/pacm/Djcgl/audit/" + row["Djcgll_BillNo"],
                data: JSON.stringify(d),
                success: function () {
                    com.message('success', "单据已通过审核");
                }
            });
        });
    };
    //this.deleteClick = self.gridEdit.deleterow;
    //删除
        this.deleteClick = function () {
            var row = self.grid.treegrid('getSelected');
            if (!row) return com.message('warning', "请选择待删除的单据！");
            com.message('confirm', "确定要删除选中的单据吗", function (b) {
                if (b) {
                    com.ajax({
                        type: 'DELETE',
                        url: "/api/pacm/Djcgl/" + row["Djcgll_BillNo"],
                        success: function () {
                            com.message('success', "删除成功");
                            self.refreshClick();
                        }
                    });
                }
            });
        }
        //保存
    this.saveClick = function () {
        self.gridEdit.ended();
        var post = {};
        post.list = self.gridEdit.getChanges(['Djcgll_BillNo', 'Djcgll_Year', 'Djcgll_Month', 'Djcgll_Dwid', 'Djcgll_Dwmc', 'Djcgll_Rdll', 'Djcgll_Scyl', 'Djcgll_Bll', 'Djcgll_Xxclw', 'Djcgll_Zfclw', 'Djcgll_Remark']);
        if (self.gridEdit.isChangedAndValid) {
            com.ajax({
                url: '/api/pacm/Djcgl/edit',
                data: ko.toJSON(post),
                success: function (d) {
                    com.message('success', '保存成功！');
                    //self.grid.queryParams({ CodeType: self.CodeType() });
                    self.gridEdit.accept();
                }
            });
        }
    };
    this.searchClick = function () {
        var param = ko.toJS(this.form);
        this.grid.queryParams(param);
    }
//    this.clearClick = function () {
//        $.each(self.form, function () { this(''); });
//        this.searchClick();
//    }
}